import { createSelector } from 'reselect'
import { SHOW_ALL, SHOW_COMPLETED, SHOW_ACTIVE } from '../redux/constants/TodoFilters'

const getFilter = state => state.filter
const getTodos = state => state.todos

export const getVisibleTodos = createSelector(
    [getFilter, getTodos],
    (filter, todos) => {
        switch (filter) {
            case SHOW_ALL:
                return todos
            case SHOW_COMPLETED:
                return todos.filter(t => t.completed)
            case SHOW_ACTIVE:
                return todos.filter(t => !t.completed)
            default:
                throw new Error('Unknown filter: ' + filter)
        }
    }
)

export const getCompletedTodoCount = createSelector(
    [getTodos],
    todos => (
        todos.reduce((count, todo) =>
                todo.completed ? count + 1 : count,
            0
        )
    )
)